<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      
      <el-form-item label="名称" prop="name">
        <el-input
          v-model="queryParams.name"
          placeholder="请输入名称"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="关键字" prop="tags">
        <el-input
          v-model="queryParams.tags"
          placeholder="请输入关键字标签"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
     
      <el-form-item label="发布者" prop="userName">
        <el-input
          v-model="queryParams.userName"
          placeholder="请输入发布者"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
    
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    
    <el-tabs  v-model="tabActiveName" @tab-click="handleTabClick">
      <el-tab-pane label="全部" name="all"></el-tab-pane>
      <el-tab-pane  v-for="dict in statusOptions" :key="dict.dictValue" :label="dict.dictLabel" :name="dict.dictValue"></el-tab-pane>
    </el-tabs>
    <el-table v-loading="loading" :data="productList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="编号" prop="id"  align="center" />
      <el-table-column label="名称" align="left" prop="name" />
      <el-table-column label="关键字" align="left" prop="tags" />
      <el-table-column label="单位" align="center" prop="unit" />
      <el-table-column label="单价" align="center" prop="price" />
      <!-- <el-table-column label="详情描述" align="center" prop="detail" /> -->
      
      <el-table-column label="图片" align="center" prop="imgUrl" >
        <template slot-scope="scope">
          <el-image v-if="scope.row.imgUrl"
            style="width: 60px; height: auto"
            :src="scope.row.imgUrl" 
            :preview-src-list="[scope.row.imgUrl]">
          </el-image>
        </template>
      </el-table-column>
      <el-table-column label="发布用户" align="left" prop="userName" />
      <el-table-column label="发布时间" align="center" prop="createTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="点赞数" align="center" prop="voteNum" />
      <el-table-column label="销量" align="center" prop="saleNum" />
      <el-table-column label="状态" align="left" prop="status" :formatter="statusFormat" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-coordinate" v-if="scope.row.status == 1"
            @click="handleApprove(scope.row)"
          >审核</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-search"
            @click="handleShow(scope.row)"
          >查看</el-button>
          <el-button
            size="mini"
            type="text" v-if="scope.row.status == 2"
            icon="el-icon-chat-line-square"
            @click="handleCommentList(scope.row)"
          >评论列表</el-button>
          
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改用户作品对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="50%" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="100px" disabled>
      
        <el-row :gutter="20"> 
          <el-col :span="12">
            <el-form-item label="名称" prop="name">
              <el-input v-model="form.name" placeholder="请输入名称" />
            </el-form-item>
            <el-form-item label="关键字标签" prop="tags">
              <el-input v-model="form.tags" placeholder="请输入关键字标签" />
            </el-form-item>
            <el-form-item label="单价" prop="price">
              <el-input v-model="form.price" placeholder="请输入单价" />
            </el-form-item>
            <el-form-item label="单位" prop="unit">
              <el-input v-model="form.unit" placeholder="请输入单位" />
            </el-form-item>
            <el-form-item label="详情描述" prop="detail">
             <el-input v-model="form.detail" type="textarea" :rows="4" placeholder="请输入内容" />
            </el-form-item>
           <el-form-item label="图片">
              <imageUpload v-model="form.imgUrl"/>
            </el-form-item>
            <el-form-item label="发布者" prop="userName">
              <el-input v-model="form.userName" placeholder="请输入发布者" />
            </el-form-item>
          </el-col>        
          <el-col :span="12">
           <el-form-item label="视频">
              <video-player  class="video-player-box"
                 ref="videoPlayer" 
                 :options="playerOptions"
                 :playsinline="true" >
              </video-player>
            </el-form-item>
          </el-col>
        </el-row>
                
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="success" @click="submitPass" v-if="approved">通 过</el-button>
        <el-button type="warning" @click="submitNotPass" v-if="approved">驳 回</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <el-dialog :title="title" :visible.sync="commentOpen" width="50%" append-to-body>
      <el-table  :data="commentList" >
        <el-table-column label="评论者" align="center" prop="userName" width="100" />
        <el-table-column label="内容" align="center" prop="content" show-overflow-tooltip />
        <el-table-column label="评论时间" align="center" prop="createTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createTime) }}</span>
        </template>
      </el-table-column>
      </el-table>
      
      <pagination
        v-show="commentTotal>0"
        :total="commentTotal"
        :page.sync="queryCommentParams.pageNum"
        :limit.sync="queryCommentParams.pageSize"
        @pagination="getCommentList"
      />
      <div slot="footer" class="dialog-footer">
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listApproveProduct, getProduct, delProduct, addProduct, updateProduct, exportProduct } from "@/api/user/product";
import { listComment} from "@/api/user/comment";
import ImageUpload from '@/components/ImageUpload';
import { videoPlayer } from 'vue-video-player';
import 'video.js/dist/video-js.css'

export default {
  name: "Product",
  components: {
    ImageUpload,
    videoPlayer
  },
  data() {
    return {
      tabActiveName: 'all',
      // 遮罩层
      loading: true,
      // 导出遮罩层
      exportLoading: false,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      playerOptions: {
          // videojs options
          autoplay: true,
          height: '300px',
          muted: true,
          fluid: true,
          preload: 'auto',
          sources: [{
            type: "video/mp4",
            src: ""
          }],
          poster: "/static/images/author.jpg",
      },
      // 非多个禁用
      multiple: true,
      approved: false,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      commentTotal: 0,
      // 用户作品表格数据
      productList: [],
      statusOptions: [],
      commentList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      commentOpen: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        categoryId: null,
        name: null,
        tags: null,
        unit: null,
        price: null,
        detail: null,
        imgUrl: null,
        limitType: null,
        limitNum: null,
        userId: null,
        userName: null,
        status: null
      },
       // 查询参数
      queryCommentParams: {
        pageNum: 1,
        pageSize: 10,
        productId: null,
        productName: null,
        content: null,
        userId: null,
        userName: null,
        avatar: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        name: [
          { required: true, message: "名称不能为空", trigger: "blur" }
        ],
      }
    };
  },
  created() {
    this.getList();
    this.getDicts("tb_user_product_status").then(response => {
      this.statusOptions = response.data;
    });
  },
  methods: {
    /** 查询用户作品列表 */
    getList() {
      this.loading = true;
      listApproveProduct(this.queryParams).then(response => {
        this.productList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.commentOpen = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        categoryId: null,
        name: null,
        tags: null,
        unit: null,
        price: null,
        detail: null,
        imgUrl: null,
        limitType: null,
        limitNum: null,
        userId: null,
        userName: null,
        createTime: null,
        status: "0"
      };
      this.resetForm("form");
      this.approved = false;
    },
    // 字典状态字典翻译
    statusFormat(row, column) {
      
      return this.selectDictLabel(this.statusOptions, row.status);
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    handleTabClick(tab, event){
      if (tab.name != 'all') {
        this.queryParams.status = tab.name;
      }else{
        this.queryParams.status = '';
      }
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加用户作品";
    },
    /** 修改按钮操作 */
    handleApprove(row) {
      this.reset();
      const id = row.id || this.ids
      getProduct(id).then(response => {
        this.form = response.data;
        this.playerOptions.sources[0].src = this.form.videoUrl;
        this.approved = true;
        this.open = true;
        this.title = "审核用户作品";
      });
    },
    /** 查看按钮操作 */
    handleShow(row) {
      this.reset();
      const id = row.id || this.ids
      getProduct(id).then(response => {
        this.form = response.data;
        this.playerOptions.sources[0].src = this.form.videoUrl;
        this.approved = false;
        this.open = true;
        this.title = "查看用户作品";
      });
    },
    /** 查看评论按钮操作 */
    handleCommentList(row) {
      this.reset();
      this.queryCommentParams.productId = row.id;
      this.getCommentList();
    },
    getCommentList(){
      listComment(this.queryCommentParams).then(response => {
        this.commentList = response.rows;
        this.commentTotal = response.total;
        this.commentOpen = true;
        this.title = "查看作品评论";
      });
    },
     /** 提交按钮 */
    submitPass: function() {
      this.form.status = 2;
      updateProduct({
        id: this.form.id,
        status: this.form.status
      }).then(response => {
        this.msgSuccess("操作成功");
        this.open = false;
        this.getList();
      });
    },
     /** 提交按钮 */
    submitNotPass: function(){
      this.form.status = 3;
      updateProduct({
        id: this.form.id,
        status: this.form.status
      }).then(response => {
        this.msgSuccess("操作成功");
        this.open = false;
        this.getList();
      });
    },
  }
};
</script>
<style scoped>
  .video-js .vjs-icon-placeholder {
      width: 100%;
      height: 100%;
      display: block;
  }
</style>